<template>
  <el-dialog :title="dialogTitle" :visible="showDialog" @close="close">
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <el-select v-model="form.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="form.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="submit">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getManagerList, addDepartment, getDepDetail, editDepartment } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: Boolean,
    oriDeps: {
      type: Array,
      required: true
    },
    currentId: {
      type: [Number, String],
      required: true
    },
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      managerList: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10个字符', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            let deps = this.oriDeps
            if (this.form.id) {
              deps = deps.filter(item => item.id !== this.form.id)
            }
            const res = deps.some(item => item.name === value)
            res ? callback(new Error('部门名称重复')) : callback()
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blue' },
          { min: 2, max: 10, message: '部门编码为2-10个字符', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            const res = this.oriDeps.some(item => {
              return item.code === value && item.id !== this.form.id
            })
            res ? callback(new Error('部门编码重复')) : callback()
          }, trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blue' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      return `${this.form.id ? '编辑' : '新增'}部门`
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
      this.form = {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      }
    },
    getManagerList() {
      getManagerList().then(res => {
        this.managerList = res
      })
    },
    submit() {
      this.$refs.form.validate(async pass => {
        if (pass) {
          this.form.id
            ? await editDepartment(this.form)
            : await addDepartment({
              ...this.form,
              pid: this.currentId
            })
          this.$emit('updateDeps')
          this.$message.success(`${this.dialogTitle}成功`)
          this.close()
        }
      })
    },
    async getDepDetail() {
      this.form = await getDepDetail(this.currentId)
    }
  }
}
</script>
